//
//  IManager+ModuleMerchantPictureUpload.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-10-1.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "IManager+ModuleMerchantPictureUpload.h"
#import "FrameworkRelativeDefines.h"
#import "NetUrlDefines.h"
#import "RequestUtlity.h"
#import "NSString+NSString_Ex_.h"
#import "TravelException.h"
#import "RCMerchantPhotoUploadDataModel.h"
#import "GloableEngineService.h"
#import "RCFeedbackDataModel.h"

#define POST_BOBY_DATA_DATA @"postBobyData"
#define POST_BOBY_DATA_KEY  @"postBobyDataKey"
#define POST_BOBY_DATA_FILE_NAME  @"postBobyDataFileName"
#define POST_BOBY_DATA_CONTENT_TYPE  @"postBobyDataContentType"

@implementation IManager(ModuleMerchantPictureUpload)

-(int64_t)requestUploadPhotoCateforyInfoWithoutType:(NSString *)shopID withDelegate:(id<IManagerDelegate>)delegate{
    if([NSString stringIsNullOrEmpty:shopID]){
        return kInvalidRequestId;
    }
    NSString *urlSuffix = [NSString stringWithFormat:kRCMerchantPhotoUploadWithoutType,shopID];
    
    NSString *url = [RequestUtlity getFormattedUrl:urlSuffix];
    
    KFLog(@"request url %@",url);
    return [[IDataProvider sharedInstance] asyncFetchDataWithUserInfo:url
                                                             userInfo:nil
                                                      withCachePolicy:IDataCachePolicyLocalFileAndNetwork
                                                   withDataParseClass:[RCMerchantPhotoUploadDataModel class]
                                                     withDataDelegate:delegate];
}

-(int64_t)requestUploadPhotoCateforyInfoWithType:(NSString *)shopID withTypeID:(NSString *)typeID withDelegate:(id<IManagerDelegate>)delegate{
    if([NSString stringIsNullOrEmpty:shopID]){
        return kInvalidRequestId;
    }
    NSString *urlSuffix = [NSString stringWithFormat:kRCMerchantPhotoUploadWithType,shopID,typeID];
    
    NSString *url = [RequestUtlity getFormattedUrl:urlSuffix];
    
    KFLog(@"request url %@",url);
    return [[IDataProvider sharedInstance] asyncFetchDataWithUserInfo:url
                                                             userInfo:nil
                                                      withCachePolicy:IDataCachePolicyLocalFileAndNetwork
                                                   withDataParseClass:[RCMerchantPhotoUploadDataModel class]
                                                     withDataDelegate:delegate];
}

-(int64_t)requestUploadOnePhotoFiles:(NSString *)shopID withDicionary:(NSDictionary *)dict withDelegate:(id<IManagerDelegate>)delegate;{
    if([NSString stringIsNullOrEmpty:shopID]){
        return kInvalidRequestId;
    }
    NSString *urlSuffix = kRCMerchantPhotoUploadFile;
    
    NSString *url = [RequestUtlity getFormattedUrl:urlSuffix];
    
    NSString *fileName = [dict objectForKey:@"filename1"];
    NSString *fileParentID = [dict objectForKey:@"fileparentid1"];
    NSString *fileTypeID = [dict objectForKey:@"filetypeid1"];
    NSString *fileTypeName = [dict objectForKey:@"filetypename1"];
    NSData *data = [dict objectForKey:@"uploadfile1"];
    
    NSString *bduss = [GloableEngineService sharedInstance].bduss;
    NSMutableDictionary *postValue = [NSMutableDictionary dictionaryWithCapacity:5];
    [postValue setObject:shopID forKey:@"shopid"];
    [postValue setObject:bduss forKey:@"bduss"];
    [postValue setObject:fileName forKey:@"filename1"];
    [postValue setObject:fileParentID forKey:@"fileparentid1"];
    [postValue setObject:fileTypeID forKey:@"filetypeid1"];
    [postValue setObject:fileTypeName forKey:@"filetypename1"];
    
    NSMutableArray *imgDataList = nil;
    if (data != nil) {
        imgDataList = [[[NSMutableArray alloc] initWithCapacity:1] autorelease];
        NSMutableDictionary *t = [[NSMutableDictionary alloc] init];
        [t setObject:data  forKey:POST_BOBY_DATA_DATA];
        [t setObject:@"image/jpeg"  forKey:POST_BOBY_DATA_CONTENT_TYPE];
        [t setObject:@"uploadfile1"  forKey:POST_BOBY_DATA_KEY];
        [t setObject:@"myphoto.jpg"  forKey:POST_BOBY_DATA_FILE_NAME];
        [imgDataList addObject:t];
        [t release];
    }
    
    return [[IDataProvider sharedInstance] asyncPostData:url postValues:postValue postDataList:imgDataList userInfo:nil withDataParseClass:[RCFeedbackDataModel class] withDataDelegate:delegate];
}

-(int64_t)requestUploadTwoPhotoFiles:(NSString *)shopID withDicionary:(NSDictionary *)dict withDelegate:(id<IManagerDelegate>)delegate{
    if([NSString stringIsNullOrEmpty:shopID]){
        return kInvalidRequestId;
    }
    NSString *urlSuffix = kRCMerchantPhotoUploadFile;
    
    NSString *url = [RequestUtlity getFormattedUrl:urlSuffix];
    
    NSString *fileName1 = [dict objectForKey:@"filename1"];
    NSString *fileParentID1 = [dict objectForKey:@"fileparentid1"];
    NSString *fileTypeID1 = [dict objectForKey:@"filetypeid1"];
    NSString *fileTypeName1 = [dict objectForKey:@"filetypename1"];
    NSData *data1 = [dict objectForKey:@"uploadfile1"];
    
    NSString *fileName2 = [dict objectForKey:@"filename2"];
    NSString *fileParentID2 = [dict objectForKey:@"fileparentid2"];
    NSString *fileTypeID2 = [dict objectForKey:@"filetypeid2"];
    NSString *fileTypeName2 = [dict objectForKey:@"filetypename2"];
    NSData *data2 = [dict objectForKey:@"uploadfile2"];
    
    NSString *bduss = [GloableEngineService sharedInstance].bduss;
    NSMutableDictionary *postValue = [NSMutableDictionary dictionaryWithCapacity:5];
    [postValue setObject:shopID forKey:@"shopid"];
    [postValue setObject:bduss forKey:@"bduss"];
    [postValue setObject:fileName1 forKey:@"filename1"];
    [postValue setObject:fileParentID1 forKey:@"fileparentid1"];
    [postValue setObject:fileTypeID1 forKey:@"filetypeid1"];
    [postValue setObject:fileTypeName1 forKey:@"filetypename1"];
    
    [postValue setObject:fileName2 forKey:@"filename2"];
    [postValue setObject:fileParentID2 forKey:@"fileparentid2"];
    [postValue setObject:fileTypeID2 forKey:@"filetypeid2"];
    [postValue setObject:fileTypeName2 forKey:@"filetypename2"];
    
    NSMutableArray *imgDataList = nil;
    if (data1 != nil || data2 != nil) {
        imgDataList = [[[NSMutableArray alloc] initWithCapacity:1] autorelease];
        
        if (data1 != nil) {
            
            NSMutableDictionary *t1 = [[NSMutableDictionary alloc] init];
            [t1 setObject:data1  forKey:POST_BOBY_DATA_DATA];
            [t1 setObject:@"image/jpeg"  forKey:POST_BOBY_DATA_CONTENT_TYPE];
            [t1 setObject:@"uploadfile1"  forKey:POST_BOBY_DATA_KEY];
            [t1 setObject:@"myphoto.jpg"  forKey:POST_BOBY_DATA_FILE_NAME];
            [imgDataList addObject:t1];
            [t1 release];
        }
        if (data2 != nil) {
            
            imgDataList = [[[NSMutableArray alloc] initWithCapacity:1] autorelease];
            NSMutableDictionary *t2 = [[NSMutableDictionary alloc] init];
            [t2 setObject:data2  forKey:POST_BOBY_DATA_DATA];
            [t2 setObject:@"image/jpeg"  forKey:POST_BOBY_DATA_CONTENT_TYPE];
            [t2 setObject:@"uploadfile1"  forKey:POST_BOBY_DATA_KEY];
            [t2 setObject:@"myphoto.jpg"  forKey:POST_BOBY_DATA_FILE_NAME];
            [imgDataList addObject:t2];
            [t2 release];
        }
    }
    
    return [[IDataProvider sharedInstance] asyncPostData:url postValues:postValue postDataList:imgDataList userInfo:nil withDataParseClass:[RCFeedbackDataModel class] withDataDelegate:delegate];
}

@end
